package com.kbt.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.kbt.activity.GoodInfoActivity;
import com.kbt.activity.LoginActivity;
import com.kbt.activity.R;
import com.kbt.activity.TeMaiKaiQiangActivity;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.model.data.NewsBean;
import com.kbt.model.data.TeMaiAndKaiQiangBean;
import com.kbt.model.holder.NewGoodItemHolder;
import com.kbt.ui.AddCarPopWindow;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.utils.CalculatePeriod;
import com.kbt.util.utils.ImageManager;
import com.kbt.util.utils.SetingImage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class NewsAdapter extends BaseAdapter {

    Activity context;
    List<NewsBean> list;

    public NewsAdapter(Activity _context, List<NewsBean> _list) {
        this.list = _list;
        this.context = _context;
    }

    @Override
    public int getCount() {

            return list.size();
    }

    @Override
    public Object getItem(int position) {
            return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    /**
     * 再次赋值
     *
     * @param mlist
     */
    public void setList(List<NewsBean> mlist) {
        list = mlist;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NewsHolder itemHolder;
        if(convertView == null){
            itemHolder = new NewsHolder();
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.news_item, null);
            itemHolder.newsTitle = (TextView) convertView.findViewById(R.id.news_title);
            itemHolder.newsTime = (TextView) convertView.findViewById(R.id.news_time);
            itemHolder.newsFrom = (TextView) convertView.findViewById(R.id.news_from);
            itemHolder.newsImage = (ImageView) convertView.findViewById(R.id.news_icon);
            convertView.setTag(itemHolder);
        }else{
            itemHolder = (NewsHolder)convertView.getTag();
        }
            NewsBean newsBean = list.get(position);

            itemHolder.newsTitle.setText(newsBean.getNews_title());
//            itemHolder.newsTime.setText(CalculatePeriod.format(getDate(newsBean.getPublish_time())));
            itemHolder.newsTime.setText("发布时间:" + newsBean.getPublish_time());
        String url = newsBean.getNews_img();
//        int screenWidth = context.getWindowManager().getDefaultDisplay().getWidth()/2-10;
//        SetingImage.setImageHeightWidth(screenWidth,url,itemHolder.newsImage);
        ImageManager.imageLoader.displayImage(Constants.ServiceInterFace.imageURL + newsBean.getNews_img(),itemHolder.newsImage , ImageManager.getOptions(
                R.drawable.loadings, R.drawable.loadings));

        return convertView;
    }
    public class  NewsHolder{
         TextView newsTitle;
         TextView newsTime;
         TextView newsFrom;
         ImageView newsImage;
    }
    //转换日期
    public Date getDate(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date=null;
        try {
             date =simpleDateFormat.parse(time);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
